<template>
  <div class="app">
    <div style="margin-top: 25px;">
      <img class="logo" src="//s.yezgea02.com/1606836859539/onpeice.png" alt="" />
    </div>
    <div style="display: flex;align-content: center;justify-content: center;">
      <span style="font-size: 18px;color: #1879e7;">[欢迎回家]</span>
    </div>
    <div style="display: flex;align-content: center;justify-content: center;margin-top: 10px;">
      <span style="font-size: 18px;color: #1879e7;">到期时间:2024-09-02 09:37:37</span>
    </div>
    <van-grid style="margin-top: 5%;">
      <van-grid-item>
        <img class="logo2" src="@/assets/icons/login.svg" alt="" />
        <span style="font-size: 20px;color: cornflowerblue;">骷</span>
      </van-grid-item>
      <van-grid-item>
        <img class="logo2" src="@/assets/icons/login.svg" alt="" />
        <span style="font-size: 20px;color: cornflowerblue;">髅</span>
      </van-grid-item>
      <van-grid-item>
        <img class="logo2" src="@/assets/icons/login.svg" alt="" />
        <span style="font-size: 20px;color: cornflowerblue;">助</span>
      </van-grid-item>
      <van-grid-item>
        <img class="logo2" src="@/assets/icons/login.svg" alt="" />
        <span style="font-size: 20px;color: cornflowerblue;">手</span>
      </van-grid-item>
    </van-grid>
    <van-collapse v-model="activeNames">
      <van-collapse-item title="免责说明" name="1" icon="comment-o">
        本程序,只为交流学习,与解放双手其余事情与作者无关.
      </van-collapse-item>
      <van-collapse-item title="使用说明" name="2" icon="fire-o">
        添加完主号后,设置规则开始刷单,正常号刷新速度设置700-1000,白号设置刷新速度2000
      </van-collapse-item>
    </van-collapse>
    <van-button type="info" style="width: 94%;left: 10px;" @click="addDiDiAccountPopup('添加主号', '0')">添加主号</van-button>
    <van-button type="info" style="width: 94%;left: 10px;" @click="addDiDiAccountPopup('添加小号', '1')">添加小号</van-button>
    <van-button type="info" style="width: 94%;left: 10px;" @click="selecAccountList()">账户列表</van-button>
    <van-tabbar v-model="active">
      <van-tabbar-item replace to="/home" name="home" icon="wap-home">抢单首页</van-tabbar-item>
      <van-tabbar-item replace to="/order?account=" name="search" icon="cash-back-record">订单记录</van-tabbar-item>
      <van-tabbar-item replace to="/my" name="setting" icon="user">个人中心</van-tabbar-item>
    </van-tabbar>
    <!-- 弹窗区 -->
    <van-popup v-model="show" round closeable position="bottom" close-icon="close" :style="{ height: '70%' }">
      <span style="font-size: 20px;justify-content: center;
      align-content: center;display: flex;margin-top: 15px;font-weight: bold;">{{ title }}</span>
      <div style="margin-top: 10%;">
        <van-field v-model="phone" left-icon="phone-o" center clearable label="手机号" placeholder="请输入手机号">
          <template #button>
            <van-button size="small" type="primary" @click="sendSmsCode">发送验证码</van-button>
          </template>
        </van-field>
        <van-cell-group>
          <van-field v-model="code" label="验证码" left-icon="comment-o" placeholder="请输入滴滴验证码" />
        </van-cell-group>
        <div style="display: flex;justify-content: center;margin-top:15px ;">
          <van-button type="info" style="width: 45%;" @click="addMinNumber">确认添加</van-button>
          <van-button type="danger" style="width: 45%;left: 5px;" @click="show = false">取消添加</van-button>
        </div>
      </div>
    </van-popup>

    <el-dialog title="小号和主号管理" :visible.sync="dialogVisible" width="98%" :before-close="handleClose">
      <span style="color: tomato">出行刷新过快设置时间为2000,重新登录获取最新状态</span>
      <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad" style="width: 100%;">
        <van-cell v-for="item in list" :key="item" style="width: 100%;">
          <span style="color:#1879e7;font-size: 15px;width: 20px;">TOKEN:{{ item.token.slice(0, 25) }}</span>
          <div>
            <span style="color:#1879e7;font-size: 15px;">账号状态:<van-tag type="success" style="font-size: 15px;">{{
              item.tokenStatus }}</van-tag></span>
          </div>
          <div>
            <span style="color:#1879e7;font-size: 15px;">账号类型:
              <van-tag type="success" style="font-size: 15px;" v-show="item.type == '0'">主号</van-tag>
              <van-tag type="success" style="font-size: 15px;" v-show="item.type == '1'">小号</van-tag></span>
          </div>
          <div>
            <span style="color:#1879e7;font-size: 15px;">刷新时长:<van-tag type="success" style="font-size: 15px;">{{
              item.sxNumber }}毫秒</van-tag></span>
          </div>
          <div style="margin-left:40%;width: 100%;">
            <van-button type="info" icon="setting-o" size="small" style="margin-left: auto;"
              @click="settingMinSx(item)">设置刷新</van-button>
            <van-button type="danger" style="left: 5px;margin-left: auto;" icon="close" size="small"
              @click="deleteToken(item)">
            <span v-if="item.type == 1">删除小号</span>
            <span v-if="item.type == 0">删除主号</span>
            </van-button>
          </div>
        </van-cell>
      </van-list>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog :title="errorMsg" :visible.sync="settingMinSxVisible" width="98%" :before-close="handleClose">
      <van-field v-model="form.phone" name="TOKEN" readonly label="TOKEN" placeholder="TOKEN" />
      <van-field v-model="form.sxNumber" name="刷新时长" label="刷新时长" placeholder="刷新时长" />
      <span slot="footer" class="dialog-footer">
        <el-button @click="settingMinSxVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateMiniNumberSxButton()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { request } from "../network/requst";
import { Dialog } from "vant";
export default {
  data() {
    return {
      // 底部菜单切换
      active: 2,
      activeNames: ["1"],
      // 表单数据
      phone: "", // 手机号
      code: "", // 验证码
      type: "", // 滴滴账户类型 0主号 1小号
      // 弹窗标题
      show: false,
      title: "",
      dialogVisible: false,
      settingMinSxVisible: false,
      // list数据
      list: [],
      loading: false,
      finished: false,
      // 设置小号刷新时长表单数据
      form: {
        sxNumber: "",
        phone: "",
      },
      // 错误消息
      errorMsg: "设置小号刷新时长",
    };
  },
  created() { },
  methods: {
    // 删除token
    deleteToken(data) {
      this.dialogVisible = false;
      Dialog.confirm({
        title: '删除小号',
        message: '是否删除该Token?',
      })
        .then(() => {
          request({
            url: "/threadManager/deleteToken",
            params: { token: data.token },
          }).then((res) => {
            if (res.data.code == 200 || res.data.code == 500) {
              Dialog.alert({
                title: "消息提示",
                message: res.data.message,
                theme: "round-button",
              });
            }
          });
        })
        .catch(() => {
          this.dialogVisible = true;
        });
    },
    // 修改小号刷新时长按钮
    updateMiniNumberSxButton() {
      if (this.form.sxNumber == "" || this.form.sxNumber == undefined) {
        Dialog.alert({
          title: "消息提示",
          message: "刷新延时不能为空.",
          theme: "round-button",
        });
        this.settingMinSxVisible = false;
        this.dialogVisible = false;
      } else {
        request({
          url: "/config/updateAccountTokenSxNumber",
          params: { phone: this.form.phone, sxNumber: this.form.sxNumber },
        }).then((res) => {
          if (res.data.code == 200 || res.data.code == 500) {
            Dialog.alert({
              title: "消息提示",
              message: res.data.message,
              theme: "round-button",
            });
            this.settingMinSxVisible = false;
            this.dialogVisible = false;
          }
        });
      }
    },
    // 设置小号刷新时间弹窗
    settingMinSx(data) {
      this.errorMsg = "设置小号刷新时长";
      if (data.type == 0) {
        this.dialogVisible = false;
        Dialog.alert({
          title: "消息提示",
          message: "主号请在首页设置刷新时长",
          theme: "round-button",
        });
      } else {
        this.settingMinSxVisible = true;
        this.form.phone = data.token;
      }
    },
    // 添加账户弹窗函数
    addDiDiAccountPopup(title, type) {
      this.show = true;
      this.title = title;
      this.type = type;
    },
    // 发送短信验证码
    sendSmsCode() {
      request({
        url: "/threadManager/sendSms",
        params: { phone: this.phone },
      }).then((res) => {
        if (res.data.code == 200 || res.data.code == 500) {
          Dialog.alert({
            title: "消息提示",
            message: res.data.description,
            theme: "round-button",
          });
        }
      });
    },
    // 添加token到数据库
    addMinNumber() {
      request({
        url: "/threadManager/loginDiDi",
        params: { phone: this.phone, code: this.code, type: this.type },
      }).then((res) => {
        if (res.data.code == 200 || res.data.code == 500) {
          Dialog.alert({
            title: "消息提示",
            message: res.data.data,
            theme: "round-button",
          });
        }
      });
    },
    // 查询小号列表
    selecAccountList() {
      this.dialogVisible = true;
      this.finished = false;
      this.list = [];
    },
    onLoad() {
      request({
        url: "/config/getUserTokenList",
        params: {},
      }).then((res) => {
        if (res.data.code == 200) {
          setTimeout(() => {
            for (let i = 0; i < res.data.data.length; i++) {
              this.list.push(res.data.data[i]);
            }
            // 加载状态结束
            this.loading = false;
            // 数据全部加载完成
            if (res.data.data.length >= res.data.data.length) {
              this.finished = true;
            }
          }, 500);
        } else {
          this.dialogVisible = false;
          Dialog.alert({
            title: "消息提示",
            message: res.data.message,
            theme: "round-button",
          });
          return;
        }
      });
    },
  },
};
</script>
<style scoped>
.logo {
  width: 250px;
  display: block;
  margin: 0 auto;
  margin-bottom: 100px;
}
</style>
